﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Text;
using System.Windows.Navigation;
using System.Xml.Linq;

namespace BaiDuAPI
{
    public partial class Search : PhoneApplicationPage
    {
        public Search()
        {
            InitializeComponent();
        }
        private string PlaceAPIKey = "4bfabb075f9024cb4990247a8f909711";
        string lat,lon;
       

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            searchTextBox.Text = "玉洁巷桥";
            lat=NavigationContext.QueryString["lat"].ToString();
            lon=NavigationContext.QueryString["lon"].ToString();         
        }

        private void searchButton_Click(object sender, RoutedEventArgs e)
        {
            string url = string.Format("http://api.map.baidu.com/place/search?&query={0}&location={1},{2}&radius=2000&output=xml&key={3}",searchTextBox.Text,lat,lon, PlaceAPIKey);
            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
            MapUpdate forecastState = new MapUpdate();
            forecastState.AsyncRequest = wr;
            wr.BeginGetResponse(new AsyncCallback(HandleForecastResponse), forecastState);
        }

         public class MapUpdate
        {
            public HttpWebRequest AsyncRequest { get; set; }
            public HttpWebResponse AsyncResponse { get; set; }
        }

         public void HandleForecastResponse(IAsyncResult asyncResult)
         {
             // get the state information
             MapUpdate forecastState = (MapUpdate)asyncResult.AsyncState;
             HttpWebRequest wr = (HttpWebRequest)forecastState.AsyncRequest;

             // end the async request
             forecastState.AsyncResponse = (HttpWebResponse)wr.EndGetResponse(asyncResult);

             System.IO.Stream streamResult;
             double newLat = 0.00;
             double newLon = 0.00;
             string address = "";

             // create a temp collection for the new forecast information for each 
             // time period
             try
             {
                 // get the stream containing the response from the async call
                 streamResult = forecastState.AsyncResponse.GetResponseStream();

                 // load the XML
                 XElement xmlMap = XElement.Load(streamResult);
                 XElement xmlCurrent = xmlMap.Descendants("results").First();
                 StringBuilder sb = new StringBuilder();

                 foreach (XElement curElement in xmlCurrent.Elements("result"))
                 {
                     try
                     {
                         address = (string)(curElement.Element("name"));
                         xmlCurrent = curElement.Descendants("location").First();
                         newLat = (double)(xmlCurrent.Element("lat"));
                         newLon = (double)(xmlCurrent.Element("lng"));
                         sb.Append(address);
                         sb.Append("\r");
                         sb.Append(newLat.ToString());
                         sb.Append(",");
                         sb.Append(newLon.ToString());
                         sb.Append("\r");
                     }
                     catch (FormatException)
                     {

                     }
                 }

                 // copy the data over

                 Deployment.Current.Dispatcher.BeginInvoke(() =>
                 {
                     displayTextBlock.Text = sb.ToString();
                 });
             }
             catch (FormatException)
             {
                 return;
             }
         }
    }
}